//
//  HMCategoryTool.m
//  HMMeituanHD
//
//  Created by apple on 2017/5/23.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "HMCategoryTool.h"
#import "HMCategoryModel.h"

static NSArray *_categories;

@implementation HMCategoryTool

+ (NSArray *)getCategories{

    if (_categories == nil) {

        //从plist中获取数据
        NSString *path = [[NSBundle mainBundle] pathForResource:@"categories.plist" ofType:nil];
        NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
        //使用yymodal进行字典转模型
        _categories = [NSArray yy_modelArrayWithClass:[HMCategoryModel class] json:dictArr];
    }
    return _categories;
}

+ (NSString *)categoryImgNameWithCategoryName:(NSString *)categoryName{
    
    //遍历分类数据,查询对应的分类图片名
    for (HMCategoryModel *category in [self getCategories]) {
        if ([category.name isEqualToString:categoryName]) {
            //获取对应分类的图片名
            return category.map_icon;
        }
        //遍历子分类名称
        for (NSString *subCategory in category.subcategories) {
            if ([subCategory isEqualToString:categoryName]) {
                
                return category.map_icon;
            }
        }
    }
    
    return @"ic_category_default";
}

@end
